<?php

class Member_account_logAction extends MemberAction
{
    /*
     pdstate 判断类型
     1.调节资金变动
     2.提现
     3.订单分成
    */

    /*
    hbstatus 判断是否为红包提现
    0.红包提现
    1.大额提现	
    */
    public function index(){
        $member_id = $this->memberInfo['id'];
        $db = D('Member_account_log');
        $this->initView();
        $db->_list($this->view, array('member_id' => $member_id), 'dateline desc', 5);
        $ModulesDB = D('Modules');
        $this->display();
    }

    //提现
    public function detail()
    {
        $member_id = $this->memberInfo['id'];
        $txstate = 1; //0.微信红包提现; 1.大额提现
//        if ($this->_get('txstate') == 1) {
//            $txstate = 1;
//        }
		
 $afflist = $payment = M('Affiliatesetting')->where(array('site_id' => 143, 'status' => 1))->find();
        $member_id = $this->memberInfo['id'];
		$afflist = unserialize($afflist['value']);
		$aff=$afflist['draw'];
		$this->assign('aff',$aff);
		$memberDB = M('member');
        //$member = $memberDB->where(array('id' => $member_id))->find();
	
		$this->assign('member',M('Member')->where(' id = '.$member_id)->find());
        $this->assign('txstate', $txstate);
        $member_user = M('member')->where(array('id' => $member_id))->find();
        $this->assign('detail_momey', $member_user['money']);
        $bank = M('member_bank')->where(array('member_id' => $member_id))->select();
		foreach($bank as $k => $v){
		$bank[$k]['bank_id']=$v['id'];
		}
        $this->assign('bank', $bank);
        $site = M('Site')->where(array('id' => __SITEID__))->find();
        $site = unserialize($site['bonusconfig']);
        //$site['mindetail'] = $site[];
        $this->assign('poundage', $site['poundage']);
        $this->assign('mindetail', $site['mindetail']);


		
        //ajax异步判断
        if (IS_AJAX) {

            $demomey = (float)$this->_post('demomey');
            if ($demomey < $site['mindetail']) {
                $this->ajaxReturn($demomey, "不能少于最小提现余额!", 1);
                exit;
            }
            if ($demomey <= 0) {
                $this->ajaxReturn($demomey, "余额不足!", 1);
                exit;
            }
            if ($demomey > $member_user['money']) {
                $this->ajaxReturn($demomey, "余额不足!", 1);
                exit;
            }
            if ($mem_brinkid == "" && $this->_post('mem_brinkid') == 1) {
                $this->ajaxReturn($demomey, "请添加提现地址!", 1);
                exit;
            }
            if ($this->_post('txstate') == 0 && $demomey * (1 - 0.01 * $site['poundage']) > 200) {
                $this->ajaxReturn($demomey, "微信红包提现扣除手续费不能超过200元!", 1);
                exit;
            }
            if ($this->_post('txstate') == 0 && $demomey * (1 - 0.01 * $site['poundage']) < 1) {
                $this->ajaxReturn($demomey, "微信红包提现扣除手续费不能小于1元!", 1);
                exit;
            }
            if ($this->_post('txstate') == 1 && $demomey > 2000) {
                $this->ajaxReturn($demomey, "大额提现不能超过2000元!", 1);
                exit;
            }
            if ($this->_post('txstate') == 1 && $demomey < 100) {
                $this->ajaxReturn($demomey, "大额提现不能小于100元!", 1);
                exit;
            }

            $this->ajaxReturn($demomey, "验证通过", 0);
            exit;
        }
        if (IS_POST) {
			
			
            
           
          
          // $this->error($member['repwd']);
            if (MD5($_POST['password']) != $member_user['repwd']) {
                $this->error('密码错误');
                exit();
            }
            $demomey = (float)$this->_post('demomey');
            $mem_brinkid = $this->_post('men_brinkid');
			$freeze_money=$this->freeze_money(__SITEID__,$member_user['id']);
			
			
			$freeze_money=$member_user['money']-$freeze_money;
			if($freeze_money<$demomey){
				
				$this->error("提现失败，其中有".$freeze_money.'元在冻结期。');
                exit;
				}
			
            if ($demomey < $site['mindetail']) {
                $this->error("不能少于最小提现余额!");
                exit;
            }
            if ($demomey <= 0) {
                $this->error("提现金额不能为零!");
                exit;
            }
            if ($demomey > $member_user['money']) {
                $this->error("余额不足!");
                exit;
            }
            if ($mem_brinkid == "" && $this->_post('txstate') == 1) {
                $this->error("请添加提现地址!");
                exit;
            }
            if ($this->_post('txstate') == 0 && $demomey * (1 - 0.01 * $_POST['fee']) > 200) {
                $this->error("微信红包提现扣除手续费不能超过200元");
                exit;
            }
            if ($this->_post('txstate') == 0 && $demomey * (1 - 0.01 * $_POST['fee']) < 1) {

                $this->error("微信红包提现扣除手续费不能小于1元");
                exit;
            }
            if ($this->_post('txstate') == 1 && $demomey > 20000) {
                $this->error("大额提现不能超过2000元");
                exit;
            }
            if ($this->_post('txstate') == 1 && $demomey < 100) {

                $this->error("大额提现不能小于100元");
                exit;
            }
				
            //初次判断这个地方可以能防止AJAX提交
            //if(IS_AJAX){return false;}
            $data = array(
                'product_id' => 0,
                'status' => 0,
                'site_id' => __SITEID__,
                'order_id' => 0,
                'remark' => $member_user['nickname']."用户，申请余额提现,提现金额:" . $demomey . "  手续费:" . $_POST['fee'] . "元",
				'total'=>$member_user['money']-$demomey,
                'dateline' => time(),
                'changenum' => $demomey- $_POST['fee'],
                'mem_brankid' => $mem_brinkid,
                'pdstate' => 2,
                'hbstatus' => $this->_post('txstate'),
                'goodsMoney' => $demomey,
				'member_id' => $member_id,
                C('TOKEN_NAME') => $_POST[C('TOKEN_NAME')]
            );
			
            //防止用户重复请求
            $Member_account_log = D('Member_account_log');
            //$mes = $Member_account_log ->up_account_log($member_id,$data);
            if (!$Member_account_log->create($data)) {
                $this->error($Member_account_log->getError());
                exit;
            }
		
            $mes = $Member_account_log->add();

            if ($mes) {
				
              
		
                $money = $member_user['money'] - $demomey;
				
                M('Member')->where(' id =  '.$member_id)->save(array('money' => $money));
                $this->success('提现成功，正提交审核！', U('Index/index'));
                exit;
            } else {
                $this->error('提现失败');
                exit;
            }
        } else {
            $this->display();
        }
    }
	
	/*多少金额是被冻结的
	 *
	 *site_id 站点id 
	 *member_id 用户id
	 *返回数字，即多少钱不可以提现。
	 */
	public function freeze_money($stie_id,$memeber_id){
		$freeze=(M('Site')->where(array('id'=>$site_id))->getField('freeze'))*86400;
		$freeze=time()-$freeze;
		$money=M('Member_account_log')->where('member_id = '.$memeber_id.' and pdstate = 4  and dateline > '.$freeze)->sum('changenum');
		
		return $money;
		
	}
    //会员分成明细
    /*
    status 状态类型
    0.待分成
    1.已分成
    */
    public function divide(){
        $this->userinfo = D('Member')->alias('m')->where(array('m.id'=>$this->memberInfo['id'],'m.site_id'=>__RELATEID__))->Field("m.*")->find();
        $this->LevelId=D('Member')->where('id = '.$this->userinfo['id'])->getfield('level');
        
            $this->stype=array('LEQ','0');//营业收益
       
        $member_id = $this->memberInfo['id'];
        $divideDB = D('Member_account_log');
        $map['member_id']=$member_id;
        if($this->stype == 1){
            $map['types']=1;
        }else{
//            $map['pdstate']=4;
        }
        //筛选条件
        $pd_time = $this->_get('pd_time');

//        $map['pdstate'] = array('eq', 3);
        if ($pd_time == 1) {
            $find_time = $this->_get('find_time');
            $month_first = date("Y-m-01", strtotime($find_time)); //当月的第一天
            $month_last = date("Y-m-d", strtotime("$month_first +1 month -1 day"));
            $month_first = strtotime($month_first);
            $month_last = strtotime($month_last);
            $map['dateline'] = array(array('gt', $month_first), array('lt', $month_last));
        }
		$map['order_id'] = array('GT','0');
		$map['pdstate'] = array('GT','1');
	  	
        if (isset($_GET['type'])) {
            $map['type'] = intval($_GET['type']);
        }
		if(isset($_GET['keywords']) && ($_GET['keywords']) !=''){
			$map['remark'] = array('like', '%'.$_GET['keywords'].'%');
			//$deep = ['deep', intval($_GET['deep'])];
		}
        $this->initView();//初始化视图
//        var_dump($map);
        $divideDB->_list($this->view, $map, 'dateline desc', 3);
//        echo '<br>';
//        var_dump($divideDB->getLastSql());
        $data = $this->sumdivide();

        $this->assign('sumdivide', $data);
        $this->display();

    }

    //添加提现地址
    public function address()
    {
        $member_id = $this->memberInfo['id'];
        if (IS_POST) {
            $_POST['member_id'] = $member_id;
            $bank = M('member_bank');
            $ret = $bank->create();
            if (!$ret) {
                $this->error($bank->getError());
            }
            $ret = $bank->add();
            if ($ret) {
                $this->success('添加成功', 'detail');
            } else {
                $this->error($bank->getError());
            }
            exit;
        }
        $this->display();
    }

    //删除提现地址
    public function deladr()
    {

        $bank_id = $this->_get('id');
        $mes = M('member_bank')->where(array('id' => $bank_id))->delete();
        if ($mes) {
            $this->success('删除成功', '');
        } else {
            //$this->error("删除失败");
        }

    }

    //提现明细
    public function detailmes()
    {

        $member_id = $this->memberInfo['id'];
        $divideDB = D('Member_account_log');
        //筛选条件
        $map['member_id'] = $member_id;
        $map['pdstate'] = array('eq', 2);

        $this->initView();//初始化视图
        $divideDB->_list($this->view, $map, 'dateline desc', 3);


        //调用分成类显示分成数据
        $affobj = $payment = M('Affiliatesetting')->where(array('site_id' => __SITEID__, 'status' => 1))->find();
        if ($affobj) {
            import("@.ORG.Affiliate." . $affobj['classname']);
            $class = $affobj['classname'];
            $Threelevel = new $class(0, __SITEID__, $member_id);
            $affdata = $Threelevel->getAffData();//dump($affdata);
        }
        if (isset($_GET['dump'])) {
            dump($affdata);
        }
        $this->assign('affdata', $affdata);


        $this->display();

    }

    //计算总分成 本月分成
    public function sumdivide()
    {

        $data = array();
        $member_id = $this->memberInfo['id'];
        //获取时间区间
        $timek = mktime(0, 0, 0, date("m"), 1, date("Y"));
        $timej = mktime(23, 59, 59, date("m"), date("t"), date("Y"));


        $divide = M('Member_account_log');
		
		
		$map1['member_id'] = $member_id;
		$map1['pdstate'] = 3;
		
		$map2['member_id'] = $member_id;
		$map2['pdstate'] = 3;
		$map2['status'] = 0;
		
		$map3['member_id'] = $member_id;
		$map3['pdstate'] = 3;
		$map3['status'] = 1;
		
		if(isset($_GET['find_time']) && $_GET['find_time'] != ''){
			
			
			$dateline = explode('-', $_GET['find_time']);
			$starttime = strtotime($dateline[0].'-'.$dateline[1].'-1 00:00:00');
			$endtime = strtotime($dateline[0].'-'.$dateline[1].'-'.$dateline[2].' 23:59:59');
			$timestr = $starttime.','.$endtime;
			
			
			$map1['dateline'] = array('between',$timestr);
			$map2['dateline'] = array('between',$timestr);
			$map3['dateline'] = array('between',$timestr);
			
		}
		
		
        //计算总分成
        $data['zdivide'] = $divide->where($map1)->Sum('changenum');
        $data['ddivide'] = $divide->where($map2)->Sum('changenum');
        $data['ydivide'] = $divide->where($map3)->Sum('changenum');
        if (isset($_GET['type'])) {
            $map['type'] = intval($_GET['type']);
        }
        //计算本月分成
        $map['member_id'] = $this->memberInfo['id'];
        $map['dateline'] = array(array('gt', $timek), array('lt', $timej));
        $map['pdstate'] = array('eq', 3);
        $data['bzdivide'] = $divide->where($map)->Sum('changenum');
        $map['status'] = array('eq', 0);
        $data['bddivide'] = $divide->where($map)->Sum('changenum');
        $map['status'] = array('eq', 1);
        $data['bydivide'] = $divide->where($map)->Sum('changenum');

        return $data;

    }
	
	 public function integral_detail()
    {
		
		
		
		 $afflist = $payment = M('Affiliatesetting')->where(array('site_id' => 143, 'status' => 1))->find();
        $member_id = $this->memberInfo['id'];
		$afflist = unserialize($afflist['value']);
		$aff=$afflist['draw'];
        $bank_db=M('Member_bank');
        $memberDB = M('member');
        $myid = $this->memberInfo['id'];
	
		$member = M('Member')->where('id = ' . $myid)->find();
        if (IS_POST) {
            
        
           $demomey = (float)$this->_post('demomey');
          // $this->error($member['repwd']);
            if (MD5($_POST['password']) != $member['repwd']) {
                $this->ajaxReturn(0,('密码错误'),1);
                exit();
       			 } 
		
		

           
		   
            if ($demomey < $site['mindetail']) {
                $this->ajaxReturn($demomey, "不能少于最小提现余额!", 1);
                exit;
            }
            if ($demomey <= 0) {
                $this->ajaxReturn($demomey, "余额不足!", 1);
                exit;
            }
            if ($demomey > $member['offline']) {
                $this->ajaxReturn($demomey, "余额不足!", 1);
                exit;
            }
           if ($mem_brinkid == "" && $this->_post('mem_brinkid') == 1) {
                $this->ajaxReturn($demomey, "请添加提现地址!", 1);
                exit;
            }
            /* if ($this->_post('txstate') == 0 && $demomey * (1 - 0.01 * $site['poundage']) > 200) {
                $this->ajaxReturn($demomey, "微信红包提现扣除手续费不能超过200元!", 1);
                exit;
            }
            if ($this->_post('txstate') == 0 && $demomey * (1 - 0.01 * $site['poundage']) < 1) {
                $this->ajaxReturn($demomey, "微信红包提现扣除手续费不能小于1元!", 1);
                exit;
            }
            if ($this->_post('txstate') == 1 && $demomey > 2000) {
                $this->ajaxReturn($demomey, "大额提现不能超过2000元!", 1);
                exit;
            }
            if ($this->_post('txstate') == 1 && $demomey < 100) {
                $this->ajaxReturn($demomey, "大额提现不能小于100元!", 1);
                exit;
            }*/

        
		
		$afflist = $payment = M('Affiliatesetting')->where(array('site_id' => __SITEID__, 'status' => 1))->find();
		
		$afflist = M('Affiliatesetting')->where(array('site_id' => __SITEID__, 'status' => 1))->find();
			 
			$afflist = unserialize($afflist['value']);
		
 			if ($this->_post('txstate') == 1 && $demomey > 20000) {
                $this->error("提现不能超过20000元!");
                exit;
            }
            if ($this->_post('txstate') == 1 && $demomey < 100) {
                $this->error("提现不能小于100元!");
                exit;
            }
			//$_POST['fee']=$afflist['draw'][$member['level']];
			
			
			  $data = array(
                'product_id' => 0,
                'status' => 0,
                'site_id' => __SITEID__,
                'order_id' => 0,
                'remark' => $member['nickname']."用户，申请积分提现,提现金额:" . $demomey . "  手续费:" . $_POST['fee'] . "元",
                'dateline' => time(),
                'changenum' => $demomey - $_POST['fee'],
                'mem_brankid' => $_POST['men_brinkid'],
				'total'=>$member['offline']-$demomey,
                'pdstate' => 2,
                'hbstatus' => '2',
                'goodsMoney' => $demomey,
				'member_id' => $member['id'],
                C('TOKEN_NAME') => $_POST[C('TOKEN_NAME')]
            );
			  
			
			$Member_account_log = D('Member_account_log');
           

			
            if (!$Member_account_log->create($data)) {
                $this->error($Member_account_log->getError());
                exit;
            }
		
            $mes = $Member_account_log->data($data)->add();
			$idata=array(
			'product_id' => 0,
              'sale_id' => 0,
                'site_id' => __SITEID__,
                'order_id' => 0,
                'remark' =>$member['nickname']."用户，申请积分提现,提现金额:" . $demomey . "  手续费:" . $_POST['fee'] .'元',
                'time' => time(),
                'offline' => $demomey - $_POST['fee'],
          		'hbstatus'=>'2',
				'total'=>$member['offline']-$demomey,

				'member_id' => $member['id'],
                C('TOKEN_NAME') => $_POST[C('TOKEN_NAME')]
			);
			
			M('Integral_log')->add($idata);
			   if ($mes) {
				
             
                //$member = $memberDB->where(array('id' => $member['id']))->find();
                $money = $member['offline'] - $demomey;
                $memberDB->where(array('id' => $member['id']))->save(array('offline' => $money));
			
                $this->success('提现成功，正提交审核！', U('Index/index'));
                exit;
            } else {
                $this->error('提现失败');
                exit;
            }
	  }
	  $data = $this->sumdivide();

      
	
		$this->assign('detail_momey',round($member['offline'],2));
      $txstate = 1;
	  $this->assign('txstate', $txstate);
      $member=M('Member')->where('id = '.$myid)->find();
      $bank_list = $bank_db->where(' member_id = ' . $myid)->select();  
	 
      $this->assign('member',$member);
      $this->assign('bank_list',$bank_list);
      $this->display();
  
	}
	
	  //会员分成明细
    /*
    status 状态类型
    0.待分成
    1.已分成
    */
    public function money(){
        $this->userinfo = D('Member')->alias('m')->where(array('m.id'=>$this->memberInfo['id'],'m.site_id'=>__RELATEID__))->Field("m.*")->find();
        $this->LevelId=D('Member')->where('id = '.$this->userinfo['id'])->getfield('level');
        if(isset($_GET['stype'])&&$_GET['stype']==1){
            //$this->stype=1;//推荐收益
        }else{
          // $this->stype=0;//营业收益
        }
        $member_id = $this->memberInfo['id'];
        $divideDB = D('Member_account_log');
        $map=' ( member_id = '.$member_id;
        if($this->stype == 1){
          //  $map.='and  types =  1';
        }else{
//            $map['pdstate']=4;
        }
        //筛选条件
        $pd_time = $this->_get('pd_time');

//        $map['pdstate'] = array('eq', 3);
        if ($pd_time == 1) {
            $find_time = $this->_get('find_time');
            $month_first = date("Y-m-01", strtotime($find_time)); //当月的第一天
            $month_last = date("Y-m-d", strtotime("$month_first +1 month -1 day"));
            $month_first = strtotime($month_first);
            $month_last = strtotime($month_last);
            $map['dateline'] = array(array('gt', $month_first), array('lt', $month_last));
        }
		
		
	  	$map.=' and order_id > 0 and  pdstate > 0  and  attr is not NULL  and status > 0 ) or (   pdstate = 0  and member_id = '.$member_id.' ) or ( pdstate = 4 and member_id = '.$member_id.' ) or ( member_id = '.$member_id.'  and hbstatus >= 1 )';
		//exit(dump($map));
        if (isset($_GET['type'])) {
            $map['type'] = intval($_GET['type']);
        }
		if(isset($_GET['keywords']) && ($_GET['keywords']) !=''){
			$map['remark'] = array('like', '%'.$_GET['keywords'].'%');
			//$deep = ['deep', intval($_GET['deep'])];
		}
        $this->initView();//初始化视图
//        var_dump($map); 
        $divideDB->_list($this->view, $map, 'dateline desc,changenum ASC', 3);
//        echo '<br>';
//        var_dump($divideDB->getLastSql());
        $data = $this->sumdivide();
			
        $this->assign('sumdivide', $data);
		
		
        $this->display();

    }


}